package platform.cmdexecutor;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;

import model.Player;
import model.Preference;
import scripts.JarFileScriptRunner;
import util.LoggingUtils;

public class MacPlatformCommandExecutor extends PlatformCommandExecutor {

	private static MacPlatformCommandExecutor	_instance;

	public static MacPlatformCommandExecutor getInstance() {
		if (_instance == null) {
			_instance = new MacPlatformCommandExecutor();
		}
		return _instance;
	}

	private MacPlatformCommandExecutor() {
	}

	@Override
	public void restartComputer() {
		JarFileScriptRunner scriptRunner = new JarFileScriptRunner("scripts/reboot_machine.sh");
		scriptRunner.executeScript();
	}

	@Override
	public boolean restartPlayer() {
		LoggingUtils.logStart("MacPlatformCommandExecutor in restartPlayer()");
		Player.getInstance().save();
		Preference.getInstance().save();
		
		String AppPath = MacPlatformCommandExecutor.getMacApplicationWithPath().trim();
		
		String[] args;
		
		if(AppPath.equals(""))
		{
			args = new String[] {
					"osascript",
					"-e",
					"do shell script \"ps ax | grep -i \\\"dmbplayer\\\" | grep -v grep | cut -c 1-6\"\nset pNum to result\ntry\ndo shell script \"kill -9 \" & pNum\nend try\ntell application \"dmbplayer\" to launch\ntell application \"dmbplayer\" to activate" };
		}
		else
		{
			args = new String[] {
					"osascript",
					"-e",
					"set dmbplayer to \""+AppPath+"\"\ntell application dmbplayer to quit\ntell application dmbplayer to activate" };
		}
		
		System.out.println("Mac command is running from here");
		
		if(args.length > 0)
		{
			for(String com : args)
			{
				System.out.println("Shell command: "+com);
				LoggingUtils.log(Level.SEVERE, com);
			}
		}
		
		try {
			Runtime.getRuntime().exec(args);
			System.out.println("Command executed");
		} catch (IOException e) {
			LoggingUtils.log(Level.SEVERE, "Exception in restart player",e);
			e.printStackTrace();
			return false;
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Exception in restart player",ex);
		}
		System.out.println("Mac command is running from here return true");
		//System.exit(1);
		return true;
	}
	
	public static String getMacApplicationWithPath()
	{
		LoggingUtils.logStart("MacPlatformCommandExecutor .getMacApplicationWithPath");
		File currentJar;
		String appPath = "";
		try {
			currentJar = new File(player.Test.class.getProtectionDomain().getCodeSource().getLocation().toURI());
			appPath = currentJar.getPath();
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			LoggingUtils.log(Level.SEVERE, "MacPlatformCommandExecutor .getMacApplicationWithPath "+e1.getMessage(), e1);
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Exception in restart player",ex);
		}
		
		
		System.out.println(appPath);
		
		if(appPath.contains(".app"))
		{
			try
			{
				int endIndex = appPath.indexOf(".app");
				System.out.println(endIndex);
				appPath = appPath.substring(0,endIndex+4);
			}
			catch(Exception e)
			{
				appPath = "";
				e.printStackTrace();
				LoggingUtils.log(Level.SEVERE, "MacPlatformCommandExecutor .getMacApplicationWithPath "+e.getMessage(), e);
			}
			
		}
		else
		{
			appPath = "";
		}
		return appPath;
	}

	@Override
	public void restartTeamViewer() {
		JarFileScriptRunner scriptRunner = new JarFileScriptRunner("scripts/restart_teamviewer_mac.sh");
		scriptRunner.executeScript();
	}
}
